package filter;

import com.baizhi.entity.Admin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Lenovo on 2019/6/17.
 */
@WebFilter(filterName = "MyFilter",urlPatterns = "/*")
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest= (HttpServletRequest) request;
        Admin a= (Admin) httpServletRequest.getSession().getAttribute("adminSession");
        if(a==null){
            //未登录,拦截
            httpServletRequest.setAttribute("msg","admin");
            httpServletRequest.getRequestDispatcher("login.jsp").forward(request,response);
        }else {
            //放行
            chain.doFilter(request,response);
        }
    }

    @Override
    public void destroy() {

    }
}
